import { createActions } from 'redux-actions'

const init = (config) => {
  const actionCreators = createActions({
    'saveUserInfo': userInfo => {
      let ui = {
        userInfo: userInfo.data.humanUser,
        userFinanceInfo: userInfo.data.userFinanceInfo,
        agentLevel: userInfo.message,
        gameData: userInfo.data.gameList,
        clubUrl: userInfo.data.clubAddress,
        hallPage: userInfo.data.marketAddress,
        isAgent: userInfo.data.owner,
      }
      window['hallPage'] = userInfo.data.marketAddress;

      if(userInfo.data.crowdJoin) {
        ui['crowdJoin'] = userInfo.data.crowdJoin
      }
      return ui;
    }, // payload creator,
    'openCreateGame': gameData => gameData, // payload creator,
    'closeCreateGame': gameData => gameData, // payload creator,
    'confClicked': data => data,
    'createGame': data => data,
    'openModal': modalContent => modalContent,
    'closeModal': data => data,
    'shareCrowdIdInvalid': data => data
  })
  const app = {
    doLoadUserInfo(dispatch, getState) {
      const url = `${config.pre}/personalCenter/${Cookies.get('areaCode')}`
      const opt = { method: 'GET' }
      fetchJson(url, opt).then(data => {
        console.error('new datatata', JSON.stringify(data))
        if (data.result == 600) {
          sessionStorage.setItem('result600', 'true')
          window.location.href = data.data //
        }
        if (data.result) {
          dispatch(actionCreators.openModal(data.message));
          return
        }
        dispatch(actionCreators.saveUserInfo(data))
      })
    },
    doLoadUserInfoNotSave() {
      const url = `${config.pre}/personalCenter/${Cookies.get('areaCode')}`
      const opt = { method: 'GET' }
      return fetchJson(url, opt)
    },
    openCreateGame: actionCreators.openCreateGame,
    closeCreateGame: actionCreators.closeCreateGame,
    openModal: actionCreators.openModal,
    closeModal: actionCreators.closeModal,
    confClicked: actionCreators.confClicked,
    saveUserInfo: actionCreators.saveUserInfo,
    createGame(dispatch, getState) {
      const {dynamics} = getState()
      let {config, gameType, gameName, createMemory} = dynamics.currentGame
      console.log(dynamics);
      var roomConf: any = {}
      config.options.forEach(conf => {
        if (!conf.muti) {
          roomConf[conf.key] = conf.selected[0]
        } else {
          roomConf[conf.key] = conf.selected
        }
      })
      if (gameName === '点子牛') {
        if (roomConf.ruleType === 6) {
          roomConf.scopeType = 1
        } else if (roomConf.ruleType === 7) {
          roomConf.scopeType = 2
        }
      }
      if (gameName === '斗公牛') {
        roomConf.ruleType = 0
      }
      if (gameName === '长沙麻将') {
        roomConf.liuliushun = roomConf.wanfa.some(wan => wan === 'liuliushun')
        roomConf.queyise = roomConf.wanfa.some(wan => wan === 'queyise')
        roomConf.banbanhu = roomConf.wanfa.some(wan => wan === 'banbanhu')
        roomConf.dasixi = roomConf.wanfa.some(wan => wan === 'dasixi')
        roomConf.santong = roomConf.wanfa.some(wan => wan === 'santong')
        roomConf.jiejiegao = roomConf.wanfa.some(wan => wan === 'jiejiegao')
        roomConf.yizhihua = roomConf.wanfa.some(wan => wan === 'yizhihua')
        roomConf.zhongtusixi = roomConf.wanfa.some(wan => wan === 'zhongtusixi')
      }
      if (gameName === '惠州麻将') {
        roomConf.daifeng = roomConf.wanfa.some(wan => wan === 'daifeng')
        roomConf.canJiHu = roomConf.wanfa.some(wan => wan === 'canJiHu')
        roomConf.menQingJiafen = roomConf.wanfa.some(wan => wan === 'menQingJiafen')
        roomConf.genzhuan = roomConf.wanfa.some(wan => wan === 'genzhuan')
        roomConf.duoHuaHu = roomConf.wanfa.some(wan => wan === 'duoHuaHu')
        roomConf.canQiDui = roomConf.wanfa.some(wan => wan === 'canQiDui')
        roomConf.magengang = roomConf.wanfa.some(wan => wan === 'magengang')

        roomConf.magengdifen = roomConf.maMode.some(wan => wan === 'magengdifen')
        roomConf.magenduipeng = roomConf.maMode.some(wan => wan === 'magenduipeng')
        roomConf.magengang = roomConf.maMode.some(wan => wan === 'magengang')
        
      }
      if (gameName === '平江扎鸟') {
        roomConf.pinghu = true
        // roomConf.liuliushun = roomConf.wanfa.some(wan => wan === 'liuliushun')
        // roomConf.queyise = roomConf.wanfa.some(wan => wan === 'queyise')
        // roomConf.banbanhu = roomConf.wanfa.some(wan => wan === 'banbanhu')
        // roomConf.dasixi = roomConf.wanfa.some(wan => wan === 'dasixi')
        // roomConf.pinghu = roomConf.wanfa.some(wan => wan === 'pinghu')
      }
      if (gameName === '平江开船') {
        roomConf.ruleType = 0
      }
      if (gameName === '跑得快') {
        //
        roomConf.gameFinal = [2, 3, 4]
      }
      if (gameName === '欢乐三张') {
        roomConf.bottomType = 1
        roomConf.compareType = roomConf.showType
        roomConf.sameIpJoin = false
        if(roomConf.playMethod.length === 1) {
          roomConf.gameMode = roomConf.playMethod.indexOf(1) === -1 ? 0 : 1;
          roomConf.autoOnOffline = roomConf.playMethod.indexOf(2) !== -1;
        } else if(roomConf.playMethod.length === 2) {
          roomConf.gameMode = 1;
          roomConf.autoOnOffline = true;
        }
      }
      const send = {roomConf, gameId: gameType}
      console.log('send', send)
      const url = `${global.config.pre}/crowd/room/join/${Cookies.get('areaCode')}`
      const opt = {
        method: 'POST',
        body: JSON.stringify(send)
      }
      localStorage.setItem(gameName + createMemory, JSON.stringify(config.options))
      fetchJson(url, opt).then(data => {
        console.error(data)
        if (data.result) {
          var appendMsg = '';
          if (data.result == 600) {
            window.location.href = data.data //
          }
          if (data.result == 100) {
            // window.location.href = '#/Mall'
            window.location.href = window['hallPage']
          }
          if (data.message === '钻石余额不足') {
            appendMsg = data.message + ',请到商城充值'

          } else {
            appendMsg = data.message
          }
          dispatch(actionCreators.openModal('创建房间失败,' + appendMsg))
          return
        }
         window.location.href = data.data
      })
    },
    queryStringExecute(dispatch, getState) {
      const roomNo = global.util.getQueryString('roomNo')
      console.log('roomNo', roomNo)
      if (roomNo) {
        let send = { roomNo }
        const url = `${config.pre}/crowd/room/join/${Cookies.get('areaCode')}`
        console.log(Cookies.get('token'));
        const opt = {
          method: 'POST',
          body: JSON.stringify(send),
        }
        fetchJson(url, opt).then(data => {
          if (data.result == 100) {
            window.location.href = window['hallPage']
          }
          if (data.result) {
            dispatch(actionCreators.openModal(data.message))
            return
          }
          window.location.href = data.data
        })
      }
      const shopping = util.getQueryString('shopping')
      if (shopping) {
        window.location.href = '#/'
      }
    },
    shareCrowdIdInvalid: actionCreators.shareCrowdIdInvalid,
  }
  return app
}

export default init